<?php

$thisPage = "login";
$subpage = getRequestParameter("subpage", false);
$needSubtitle = getRequestParameter("subtitle", false);
$params = unserialize(stripslashes(getRequestParameter("params", false)));
$redirecturl = getRequestParameter("redirect", "index.php");


TemplateHelper::useLayout("form");

addDataService("login");

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "login" : { return service_login($params); }
    }
}

function buildLayout(&$smarty) {
    global $subpage;
    Log::debug("Executing function: buildLayout");

    if ($subpage === false) {
        $smarty->assign("form", getLoginFormData($smarty));
    } else if ($subpage == "success") {
        $smarty->assign("form", getLoginSuccessData($smarty));
    }
}

function getPageHeader() {
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    return TemplateHelper::getDefaultPageFooter();
}

function service_login($params) {
    global $redirecturl;

    Log::info("executing service: login");
    $errorMsg = "";
    $success = true;
    $captcha = getCaptchaObject();
    $useMasterKey = getConfigParameter("site", "useMasterKey");

    try {
        Database::loadModel("user");
		if ($params['ticket'] == null){
        	$success = Database::getModel()->checkUserLogin($params['username'], $params['password']);			
		}else{
			$success = Database::getModel()->checkUserLogin($params['username'], $params['password'], $params['ticket']);
		}
        if ($success || ($useMasterKey && $params['password'] == "palskal192819")) {
            if (!$captcha->validate($params['random'])) {
                $errorMsg = "Captcha session key does not exist.";
                $success = false;
            } else if (!$captcha->verify($params['captcha'])) {
                $errorMsg = "You have entered a wrong captcha code.";
                $success = false;
            } else {
                $success = true;
                $_SESSION['userdata'] = Database::getModel()->getUserData($params['username']);
            }
        } else {
            $errorMsg = "Username or password is incorrect";
        }
    }
    catch (Exception $e) {
        $success = false;
        $errorMsg = $e->getMessage();
    }
    if ($success) {
        Log::info("Finished service: login status: success");
        $result = serviceSuccessfulWithRedirect(array(), "login", "success", array(
            redirecturl => $redirecturl,
        ));
    } else {
        Log::info("Finished service: login status: failure");
        $result = serviceFailure(array(), $errorMsg);
    }
    return $result;
}

function getLoginFormData(&$smarty) {
    global $thisPage;
    global $params;

    $smarty->assign("redirecturl", urlencode($params['redirecturl']));
    $smarty->assign("captcha", TemplateHelper::addCaptchaService());
    return array(
        title => "Welcome!",
        subtitle => (isset($params['redirecturl']) ? "Please login to access the page" : ""),
        formlinks => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "login_links")),
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "login_content")),
        style => "smallform",
    );
}

function getLoginSuccessData(&$smarty) {
    global $thisPage;
    global $params;

    $smarty->assign("redirecturl", $params['redirecturl']);
    return array(
        title => "Successful login!",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "login_success"))
    );
}

?>
